//登录
var close = document.querySelector(".close");
var lg_text = document.getElementById("lg_text");
var rg_text = document.getElementById("rg_text");
var lg = document.getElementById("lg");
var log_page = document.querySelector(".log_page");
var rg_page = document.querySelector(".rg_page");
var logandrg = document.querySelector(".logandrg");
var rg = document.getElementById("rg");
var log_button = document.querySelector(".log_button");
var rg_button = document.querySelector(".rg_button");
var role;
var no_log = document.querySelector(".no_log");
var user = document.querySelector(".user");

//封装定时器的函数
function tips(color, text) {
  document.querySelector(".tips_content").style.display = "block";
  document.querySelector(".tips_content").style.color = color;
  document.querySelector(".tips_content").innerHTML = text;
  setTimeout(function () {
    document.querySelector(".tips_content").style.display = "none";
  }, 3000);
}

//登录注册按钮
log_button.onclick = function () {
  logandrg.style.display = "block";
  log_page.style.display = "block";
  rg_page.style.display = "none";
};

rg_button.onclick = function () {
  logandrg.style.display = "block";
  log_page.style.display = "none";
  rg_page.style.display = "block";
};

//注册
rg.onclick = function () {
  var role;
  console.log(document.getElementById("rg_username").value);
  console.log(document.getElementById("rg_password").value);
  console.log(role);

  //判断身份是否选择

  if (document.getElementById("student").checked) {
    role = document.getElementById("student").value;
    console.log(role);
  } else {
    if (document.getElementById("teacher").checked) {
      role = document.getElementById("teacher").value;
      console.log(role);
    } else {
      document.querySelector(".tips_content").style.display = "block";
      document.querySelector(".tips_content").style.color = "red";
      document.querySelector(".tips_content").innerHTML = "请选择你的身份";
      setTimeout(function () {
        document.querySelector(".tips_content").style.display = "none";
      }, 3000);
    }
  }
  axios
    .post("http://120.24.80.83:3000/register", {
      username: document.getElementById("rg_username").value,
      password: document.getElementById("rg_password").value,
      role: role,
    })
    .then(function (response) {
      console.log(response);
      if (response.data.msg == "注册成功") {
        //提示框
        tips("green", "注册成功");
        log_page.style.display = "block";
        rg_page.style.display = "none";
      } else {
        tips("red", "该用户已注册，请登录");
        log_page.style.display = "block";
        rg_page.style.display = "none";
      }
    })
    .catch(function (error) {
      console.log(error);
    });
};

//按钮
close.onclick = function () {
  logandrg.style.display = "none";
};

lg_text.onclick = function () {
  log_page.style.display = "block";
  rg_page.style.display = "none";
};

rg_text.onclick = function () {
  log_page.style.display = "none";
  rg_page.style.display = "block";
};

//

document.getElementById("lg_password").value = localStorage.getItem("password"); //记住密码
//登录
lg.onclick = function () {
  //记住密码功能

  var ch = document.getElementById("check").value;
  if (ch !== "") {
    var pdd = document.getElementById("lg_password").value;
    if (typeof Storage !== "undefined") {
      localStorage.setItem("password", pdd);
    } else {
      localStorage.removeItem("password", pdd);
    }
  }

  axios
    .get("http://120.24.80.83:3000/user/isRegistered", {
      params: {
        username: document.getElementById("lg_username").value,
      },
    })
    .then(function (response) {
      console.log(response);
      console.log(response.data.msg);
      if (response.data.msg == "该用户已经注册了") {
        axios
          .post("http://120.24.80.83:3000/login", {
            username: document.getElementById("lg_username").value,
            password: document.getElementById("lg_password").value,
          })
          .then(function (response) {
            if (response.data.msg == "登录成功") {
              logandrg.style.display = "none";
              no_log.style.display = "none";
              user.style.display = "block";
              var token = response.data.data.token;
              localStorage.setItem("token", token);
              tips("green", "登录成功");
              //获取用户信息

              axios.defaults.headers.common["Authorization"] = token;
              axios
                .get("http://120.24.80.83:3000/user/getUserInfo")
                .then(function (response) {
                  console.log(response.data.msg);
                  console.log(response.data.data.avatar);
                  if (response.data.msg == "获取成功") {
                    no_log.style.display = "none";
                    user.style.display = "block";
                  }
                  localStorage.setItem(
                    "response.data.data.avatar",
                    response.data.data.avatar
                  );
                  localStorage.setItem(
                    "response.data.data.username",
                    response.data.data.username
                  );
                  document.querySelector(".username").innerHTML =
                    localStorage.getItem("response.data.data.username");

                  document.querySelector(".header_img").src =
                    localStorage.getItem("response.data.data.avatar");
                })
                .catch(function (error) {
                  console.log(error);
                });
            }
            if (response.data.msg == "用户名或密码错误") {
              //提示框
              tips("red", "用户名或密码错误");
            }
          })
          .catch(function (error) {
            console.log(error);
          });
      } else {
        //提示框
        tips("red", "您还没有请注册，请注册");
        log_page.style.display = "none";
        rg_page.style.display = "block";
      }
    })
    .catch(function (error) {});
};

//轮播图
window.onpageshow = function () {
  //获取轮播图
  axios
    .get("http://120.24.80.83:3000/banners")
    .then(function (response) {
      console.log(response);
    })
    .catch(function (error) {
      console.log(error);
    });
};


var btn=document.querySelector('.btn')
btn.mouseenter = function (){
  btn.style.display="block"
}
//轮播图
var list = document.querySelector('.list')
var items = document.getElementsByClassName("item");
var btn=document.querySelector('.btn')
var goPre = document.getElementById("gePre");
var goNext = document.getElementById("geNext");
var number=0;

goNext.onclick = function (){
  number++;
  animate(list,-number*1200)
}

//获取用户信息
window.onload = function () {
  //验证token是否失效

  axios.defaults.headers.common["Authorization"] =
    localStorage.getItem("token");
  axios
    .post("http://120.24.80.83:3000/refresh")
    .then(function (response) {
      console.log(response.data.msg);
      if (response.data.msg == "登录失效") {
        //提示框
        tips("red", "登录失效,请重新登录");
      } else {
        localStorage.setItem(
          "response.data.data.avatar",
          response.data.data.avatar
        );
        localStorage.setItem(
          "response.data.data.username",
          response.data.data.username
        );
        document.querySelector(".username").innerHTML = localStorage.getItem(
          "response.data.data.username"
        );
        document.querySelector(".header_img").src = localStorage.getItem(
          "response.data.data.avatar"
        );
      }
    })
    .catch(function (erroe) {
      console.log;
    });

  //获取用户信息

  axios.defaults.headers.common["Authorization"] =
    localStorage.getItem("token");
  axios
    .get("http://120.24.80.83:3000/user/getUserInfo")
    .then(function (response) {
      console.log(response.data.msg);
  
      if (response.data.msg == "获取成功") {
        no_log.style.display = "none";
        user.style.display = "block";
      }
     localStorage.setItem(
        "response.data.data.avatar",
        response.data.data.avatar
      );
      localStorage.setItem(
        "response.data.data.username",
        response.data.data.username
      );
      document.querySelector(".username").innerHTML = localStorage.getItem(
        "response.data.data.username"
      );
      document.querySelector(".header_img").src = localStorage.getItem(
        "response.data.data.avatar"
      );
    })
    .catch(function (error) {
      console.log(error);
    });
};


//退出
document.querySelector(".Thuichu").onclick = function () {
  no_log.style.display = "block";
  user.style.display = "none";
  localStorage.removeItem("response.data.data.avatar");
  localStorage.removeItem("response.data.data.username");
  localStorage.removeItem("token");
};
